{# templates/form/custom_types.html.twig #}

{% block module_field_row %}
    <div class="form-group" id="sonata-ba-field-container-module">
        <label class="control-label" for="module">
            模块
        </label>
        <div class="sonata-ba-field sonata-ba-field-standard-natural">
            {% for module in admin_modules %}
                <div class="module-group box-body" style="padding: 10px 0px;">
                    <div class="group-info" style="background: #f7f7f7;padding: 10px">
                        <div class="icheckbox_minimal-blue" aria-checked="true" aria-disabled="false">
                            <input type="checkbox" class="minimal module-all" module_id="{{ module.id }}">
                            <label style="margin-left: 10px">{{ module.name }}</label>
                        </div>
                    </div>
                    {% if module.children|length > 0 %}
                        <div class="row" style="padding: 15px 0px 15px 46px;">
                            {% for item in module.children %}
                                <div class="col-sm-4 col-md-2" style="padding-bottom: 10px">
                                    <div class="icheckbox_minimal-blue" aria-checked="true" aria-disabled="false">
                                        <input type="checkbox" class="minimal module-item" name="module_idx[{{ module.id }}][]" value="{{ item.id }}">
                                        <span style="margin-left: 10px">{{ item.name }}</span>
                                    </div>
                                </div>
                            {% endfor %}
                        </div>
                    {% endif %}
                </div>
            {% endfor %}
        </div>
        <script>
            {# 初始赋值 #}
            let module_ids = {% if module_ids is null %}[]{% else %}{{ module_ids }}{% endif %};
            $('input.module-item').each(function (index, item) {
                if ($.inArray(parseInt(item.value), module_ids) != -1) {
                    $(this).iCheck('check');
                }
            });
            {# 全选/反选 #}
            $('input.module-all').on('ifClicked', function (event) {
                let module_id = $(this).attr('module_id');
                if (event.target.checked) {
                    $("input[name='module_idx[" + module_id + "][]']").iCheck('uncheck');
                } else {
                    $("input[name='module_idx[" + module_id + "][]']").iCheck('check');
                }
            });
        </script>
    </div>
{% endblock %}

{% block text_errors %}
    {% if errors|length > 0 %}
        <div class="help-block sonata-ba-field-error-messages">
            <ul class="list-unstyled">
                {% for error in errors %}
                    <li><i class="fa fa-exclamation-circle" aria-hidden="true"></i>
                        {{ error.message }}
                    </li>
                {% endfor %}
            </ul>
        </div>
    {% endif %}
{% endblock %}

{# ... here you will add the Twig code ... #}